export default function AMap(plugins) {
  return new Promise((resolve, reject) => {
    if (window.AMap) {
      resolve(window.AMap)
    } else {
      const key = 'eed2ab9276e5e25e6b9eae891ab3dc8f'
      var script = document.createElement('script')
      script.type = 'text/javascript'
      script.async = false
      script.src = '//webapi.amap.com/maps?v=2.0&key=' + key + '&callback=onAMapCallback&plugin=Map3D,AMap.DistrictLayer,AMap.Scale,AMap.ToolBar,AMap.MarkerCluster,AMap.BezierCurveEditor,AMap.Autocomplete,AMap.PlaceSearch,AMap.Geocoder,AMap.IndexCluster'
      if (plugins) script.src += '&plugin=' + plugins.join(',')
      script.onerror = reject
      document.head.appendChild(script)

      // 高德地图UI组件引入
      var newScript2 = document.createElement('script')
      newScript2.type = 'text/javascript'
      newScript2.async = false
      newScript2.src = 'https://a.amap.com/jsapi_demos/static/data/district.js'
      document.head.appendChild(newScript2)

      // 高德地图UI组件引入
      /* var newScript1 = document.createElement('script')
      newScript1.type = 'text/javascript'
      newScript1.async = false
      newScript1.src = 'https://a.amap.com/jsapi_demos/static/data/community.js'
      document.head.appendChild(newScript1) */

      // 高德地图UI组件引入
      var newScript = document.createElement('script')
      newScript.type = 'text/javascript'
      newScript.async = false
      newScript.src = '//webapi.amap.com/ui/1.1/main.js?v=1.1.1'
      document.head.appendChild(newScript)
    }
    window.onAMapCallback = () => {
      resolve(window.AMap)
    }
  })
}
